package com.helen.ui.newui;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.coorchice.library.SuperTextView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.helen.adapter.MyRecoveryListAdapter;
import com.helen.db.UserHelper;
import com.helen.entity.MyRecoveryEntity;
import com.helen.global.Constants;
import com.helen.shopping.R;
import com.helen.ui.BaseActivity;
import com.helen.utils.ImageLoadUtil;
import com.helen.utils.JsonUtil;
import com.helen.utils.MyLog;
import com.helen.utils.PhoneUtils;
import com.helen.utils.tiputils.MToast;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yang on 2019/3/25
 */
public class MyRecoveryCarActivity extends BaseActivity {
    public static final int BUY_SUCCESS = 0x123;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_detail)
    SuperTextView tvDetail;
    @BindView(R.id.img)
    ImageView img;
    private List<MyRecoveryEntity> list = new ArrayList();
    private MyRecoveryListAdapter myRecoveryListAdapter;
    private View errorView;
    private int page = 1;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_my_recovery_car;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) rvList.getParent(), false);
        myRecoveryListAdapter = new MyRecoveryListAdapter(list);
        swipeRefresh.setRefreshing(true);
        swipeRefresh.setColorSchemeColors(getResources().getColor(R.color.appThemeColor));
        rvList.setLayoutManager(new LinearLayoutManager(MyRecoveryCarActivity.this));
        rvList.setAdapter(myRecoveryListAdapter);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        myRecoveryListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        }, rvList);

        myRecoveryListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MyRecoveryEntity myRecoveryEntity = (MyRecoveryEntity) adapter.getData().get(position);
                Intent ti = new Intent(MyRecoveryCarActivity.this, MyRecoveryCarDetailActivity.class);
                ti.putExtra("carId", myRecoveryEntity.getId());
                ti.putExtra("name", myRecoveryEntity.getName());
                startActivity(ti);

            }
        });
        myRecoveryListAdapter.setOnItemBuyListener(new MyRecoveryListAdapter.OnSetItemBuyListener() {
            @Override
            public void setOnBuyListener(MyRecoveryEntity myRecoveryEntity, int position) {
                Intent ti = new Intent(MyRecoveryCarActivity.this, BuyRecoveryCarActivity.class);
                ti.putExtra("myRecoveryEntity", myRecoveryEntity);
                startActivityForResult(ti, BUY_SUCCESS);
            }
        });
        refresh();
    }

    @Override
    protected void updateViews() {

    }

    private void refresh() {
        page = 1;
        getData();
    }


    private void loadMore() {
        page++;
        getData();
    }

    private void getData() {
        mMProgressDialog.show();
        HttpParams params = new HttpParams();
        params.put("id", UserHelper.getInstance().getloginEntity(MyRecoveryCarActivity.this).getId());
        params.put("page", page + "");
        params.put("size", "10");
        EasyHttp.post(Constants.API_JSONAPI_MY_CAR_LIST)
                .params(params)
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onError(ApiException e) {
                        mMProgressDialog.dismiss();
                        swipeRefresh.setRefreshing(false);
                        MToast.makeTextShort(MyRecoveryCarActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(String s) {
                        MyLog.d("yang", "s==" + s);
                        mMProgressDialog.dismiss();
                        swipeRefresh.setRefreshing(false);
                        String data = PhoneUtils.checkResponseFlag2(MyRecoveryCarActivity.this, s, TAG);
                        if (data != null) {
                            list = new Gson().fromJson(data, new TypeToken<List<MyRecoveryEntity>>() {
                            }.getType());
                            String imgUrl = JsonUtil.getJsonValuesString(s,"imgUrl");
                            ImageLoadUtil.displayImage(MyRecoveryCarActivity.this, img, imgUrl, R.mipmap.app_logo, R.mipmap.app_logo);
                            if (page == 1) {
                                myRecoveryListAdapter.setNewData(list);
                            } else {
                                myRecoveryListAdapter.addData(list);
                            }
                            if (list.size() < 10) {
                                myRecoveryListAdapter.loadMoreEnd(false);
                                myRecoveryListAdapter.setEmptyView(errorView);
                            } else {
                                myRecoveryListAdapter.loadMoreComplete();
                            }

                        }
                    }
                });

    }


    @OnClick({R.id.img_back, R.id.tv_detail})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_detail:
                startActivity(new Intent(MyRecoveryCarActivity.this, RecoveryCarMoneyDetailActivity.class));
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            switch (requestCode){
                case BUY_SUCCESS:
                    refresh();
                    break;
            }
        }
    }
}
